package net.starwifi.duobao.web;

import net.starwifi.duobao.SettingUtils;
import net.starwifi.duobao.entity.*;
import net.starwifi.duobao.mvc.BaseController;
import net.starwifi.duobao.service.*;
import net.starwifi.duobao.utils.AESCrypto;
import net.starwifi.duobao.utils.HttpRequester;
import net.starwifi.duobao.utils.HttpRespons;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.net.URLEncoder;
import java.util.*;

/**
 * 微信活动
 * Created by Administrator on 2015/8/14.
 */
@Controller("apiDdcreditController")
@RequestMapping("/ddcredit")
public class DdCreditController extends BaseController {


@Autowired
MemberService memberService;
    /**
     * 分享成功后扫码界面
     */
    @RequestMapping(value = "/Open", method = RequestMethod.GET)
    public String Open(ModelMap model, RedirectAttributes redirectAttributes)  throws Exception{
         Member member=memberService.getCurrent();
        if (isValid(member)) {
            return "redirect:/dbs/index.html";
        }
        String v = member.getMobile();
        String key = SettingUtils.get("dd_Key");
        String Url = SettingUtils.get("dd_Url");
        try {
            HttpRequester request = new HttpRequester();
            request.setDefaultContentEncoding("utf-8");
            String ev = URLEncoder.encode(AESCrypto.encrypt(v, key));
            Map<String, String> params = new HashMap<String, String>();
            params.put("pm", ev);
            HttpRespons hr = request.sendGet(Url, params);
            addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
            return "redirect:"+hr.getUrlString();

        }catch (Exception e) {
            e.printStackTrace();
        }


        return "redirect:/dbs/index.html";
    }



}
